/* 
 * Copyright TLR Team, Inc. All Rights Reserved.
 */

TLR.controllers.controller("MainViewCtrl", function ($scope, UserSrv) {
    $(window).resize(function () {
        $scope.$broadcast("WindowResize");
    });
    $scope.getColorOfPM25 = function (pm25) {
        if (pm25 >= 0 && pm25 <= 75)
            return "#3A9AFD";
        else if (pm25 > 75 && pm25 <= 150)
            return "#f0e814";
        else if (pm25 >= 151 && pm25 <= 250)
            return "#e02d1c";
        else
            return "#9F56FF";
    };
    $scope.getColorOfTVOC = function (tvoc) {
        if (tvoc >= 0 && tvoc <= 0.6) {
            return "#3A9AFD";
        } else if (tvoc > 0.6 && tvoc <= 1.8) {
            return "#f0e814";
        } else {
            return "#e02d1c";
        }
    };
    $scope.getCarColor = function (status) {
        if (status === 1) {
            return "#31cd31";
        }
        return "#e02d1c";
    };
    $scope.getHealthColor = function (point) {
        if (point >= 80 && point <= 100)
            return "#3A9AFD";
        else if (point >= 70 && point < 80)
            return "#f0e814";
        else if (point >= 60 && point < 70)
            return "#e02d1c";
        else if (point < 60)
            return "#9F56FF";
    };
    $scope.user = {};
    $scope.onUserChangePasswordSave = function (user, confirmPassword) {
        if (!user.password || user.password === "") {
            alert("旧密码不能为空");
            return;
        }
        if (user.newpassword !== confirmPassword) {
            alert("新密码输入不一致");
            return;
        }
        UserSrv.changepwd($scope.user, function (res) {
            if (res.code === 0) {
                alert("修改成功");
                $('#UserPasswordModal').modal('hide');
                $scope.user = {};
            } else {
                alert(res.message);
            }
        });
    };
})
.controller("NavViewCtrl", function () {
})
.controller("LoginViewCtrl", function ($scope, $location, UserSrv) {
    $scope.user = {};
    $scope.dashboardLogin = function () {
        UserSrv.login($scope.user, function (res) {
            if (res.code === 0) {
                window.location.href = $location.absUrl().substring(0, $location.absUrl().lastIndexOf("/"));
            } else {
                $scope.LoginMessage = res.message;
            }
        });
    };
    $scope.Login_KeyUp = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode === 13) {
            $scope.dashboardLogin();
        }
    };
});